package com.zmc.lostfound.auth.controller;

import com.zmc.lostfound.auth.entity.PermissionEntity;
import com.zmc.lostfound.auth.service.PermissionService;
import com.zmc.lostfound.auth.vo.PermissionSaveVO;
import com.zmc.lostfound.common.utils.PageParams;
import com.zmc.lostfound.common.utils.PageResult;
import com.zmc.lostfound.common.utils.Result;
import com.zmc.lostfound.common.valid.AddGroup;
import com.zmc.lostfound.common.valid.UpdateGroup;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author ZMC
 * @description 权限菜单管理接口
 * @email 2869581855@qq.com
 * @date 2023-07-05 15:09:50
 */
@RestController
@RequestMapping("/permission")
@Api(value = "权限菜单管理接口", tags = "权限菜单管理接口")
public class PermissionController {

    private final PermissionService permissionService;

    public PermissionController(PermissionService permissionService) {
        this.permissionService = permissionService;
    }

    @ApiOperation("分页模糊查询权限信息")
    @PostMapping("/query/page")
    public Result queryPage(@RequestBody @Validated PageParams pageParams) {
        PageResult pageResult = permissionService.queryPage(pageParams);
        return Result.ok("查询成功").put("pageResult", pageResult);
    }

    @ApiOperation("按主键id查询权限信息")
    @GetMapping("/query/infoById/{id}")
    public Result queryInfoById(@PathVariable("id") Long id) {
        PermissionEntity permissionInfo = permissionService.queryInfoById(id);
        return Result.ok("查询成功").put("permissionInfo", permissionInfo);
    }

    @ApiOperation("按用户id查询权限信息")
    @GetMapping("/query/infoByUserId/{userId}")
    public Result queryInfoByUserId(@PathVariable("userId") Long userId) {
        List<PermissionEntity> permissionInfoList = permissionService.queryInfoByUserId(userId);
        return Result.ok("查询成功").put("permissionInfoList", permissionInfoList);
    }

    @ApiOperation("按角色id查询所拥有权限信息")
    @GetMapping("/query/infoByRoleId/{roleId}")
    public Result queryInfoByRoleId(@PathVariable("roleId") Long roleId) {
        List<PermissionEntity> permissionInfoList = permissionService.queryInfoByRoleId(roleId);
        return Result.ok("查询成功").put("permissionInfoList", permissionInfoList);
    }

    @ApiOperation("按角色id查询所有权限信息")
    @PostMapping("/query/infoByRoleIdWithAll/{roleId}")
    public Result queryInfoByRoleIdWithAll(@PathVariable("roleId") Long roleId, @RequestBody @Validated PageParams pageParams) {
        PageResult pageResult = permissionService.queryInfoByRoleIdWithAll(roleId, pageParams);
        return Result.ok("查询成功").put("pageResult", pageResult);
    }

    @ApiOperation("新增权限信息")
    @PostMapping("/insertPermission")
    public Result insertPermission(@RequestBody @Validated(value = {AddGroup.class}) PermissionSaveVO permissionSaveVO) {
        return permissionService.insertPermission(permissionSaveVO);
    }

    @ApiOperation("修改权限信息")
    @PutMapping("/updatePermission")
    public Result updatePermission(@RequestBody @Validated(value = {UpdateGroup.class}) PermissionSaveVO permissionSaveVO) {
        return permissionService.updatePermission(permissionSaveVO);
    }

    @ApiOperation("按权限id删除权限信息")
    @DeleteMapping("/delete/{permissionId}")
    public Result deleteByPermissionId(@PathVariable("permissionId") Long permissionId) {
        return permissionService.deleteByPermissionId(permissionId);
    }

}
